<template>
  <WidgetContainer :widget-id="config.id">
    <FormItemWidget :config="config" :rowIndex="rowIndex" :parentId="parentId">
      <el-select :class="config.attributes?.cssClass" v-model="fieldValue" :placeholder="config.attributes.placeholder"
        :disabled="config.attributes.disabled" :size="config.attributes.size" :multiple="config.attributes.multiple"
        :style="{ width: config.attributes.width }" @change="handleChange">
        <el-option v-for="option in config.options" :key="option.value" :label="option.label" :value="option.value" />
      </el-select>
    </FormItemWidget>
  </WidgetContainer>
</template>

<script setup>
import { ElSelect, ElOption } from 'element-plus';
import { inject, onMounted } from 'vue'
import { executeEventHandler } from '@/utils/eventExecutor'
import { useFieldBinding } from '@/utils/useFieldBinding'
import { getDict } from '@/api/getDict';

const formData = inject('formData')
const widgetList = inject('widgetList')

const props = defineProps({
  config: Object,
  rowIndex: Number,
  parentId: String
})

const { fieldValue } = useFieldBinding(formData, props.config, {
  rowIndex: props.rowIndex,
  parentId: props.parentId
})

onMounted(() => {
  executeEventHandler(props.config.events?.onMounted,
    { widgetConfig: props.config, formData: formData.value, widgetList: widgetList.value }
  )
  // 获取字典值
  getDict(props.config)
})

const handleChange = (value) => {
  executeEventHandler(props.config.events?.onChange,
    { widgetConfig: props.config, formData: formData.value, widgetList: widgetList.value, value }
  )
}
</script>

<style scoped>
.el-select {
  width: 100%;
}
</style>